package com.pro.control;

import com.pro.entity.JsonData;
import com.pro.entity.UserCoupon;
import com.pro.service.UserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.logging.Logger;

/**
 * @ClassName: CouponController
 * @Author: 笑笑
 * @DATE: 2021/11/18
 * @TIME: 下午 08:49
 */
@RestController
@RequestMapping("/UserCoupon")
public class UserCouponController {
    @Autowired
    private UserCouponService userCouponService;

    //查询所有优惠券信息--表
    @RequestMapping("/getAllTable")
    public JsonData getAllTable(UserCoupon userCoupon) {
        Logger.getGlobal().info(String.valueOf(userCoupon));
        List<UserCoupon> allTable = userCouponService.getAllTable(userCoupon);
        return allTable.size() > 0 ? JsonData.buildSuccess(allTable) : JsonData.buildError("没有查到数据");
    }

    //查询所有优惠券信息--表
    @RequestMapping("/getById")
    public JsonData getById(Integer couponId) {
        UserCoupon byId = null;
        try {
            byId = userCouponService.getById(couponId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byId != null ? JsonData.buildSuccess(byId) : JsonData.buildError("没有查到数据");
    }

    //根据id删除优惠券
    @RequestMapping("/deleteUserCoupon")
    public JsonData deleteUserCoupon(Integer couponId) {
        Boolean byId = null;
        try {
            byId = userCouponService.deleteUserCoupon(couponId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.buildSuccess(byId);
    }

    //添加优惠券
    @RequestMapping("/addUserCoupon")
    public JsonData addUserCoupon(UserCoupon userCoupon) {
        Boolean aBoolean = userCouponService.addUserCoupon(userCoupon);
        return JsonData.buildSuccess(aBoolean);
    }

    //根据id修改优惠券
    @RequestMapping("/updateUserCoupon")
    public JsonData updateUserCoupon(UserCoupon userCoupon) {
        Boolean aBoolean = null;
        try {
            aBoolean = userCouponService.updateUserCoupon(userCoupon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.buildSuccess(aBoolean);
    }
}
